<template>
  <div>
    <div class="head">
      <div class="imgbox">
        <img :src="info.coverImgUrl" alt="" />
      </div>
      <div class="info">
        <h2><span class="type">歌单</span>{{ info.name }}</h2>
        <div class="creator">
          <img :src="creator.avatarUrl" alt="" /><span>{{
            creator.nickname
          }}</span>
          <span>{{ getDate(info.createTime) }}</span>
        </div>
        <div class="buttons">
          <span class="play">
            <i class="el-icon-caret-right"></i> 播放全部</span
          >
          <span class="etc">收藏</span>
          <span class="etc">分享</span>
          <span class="etc">下载全部</span>
        </div>
        <div class="desc">
          简介：<span>
            {{info.description}}
          </span>
        </div>
        <div class="count">
          <span>歌曲：{{ info.trackCount }}首</span
          ><span>播放：{{ info.playCount }}次</span>
        </div>
      </div>
    </div>
    <div class="list">
      <div class="bar">
        <div>
          <span @click="tolink('songlist')" :class="{active:isActive('/songlist')}">歌曲列表</span>
          <span @click="tolink('comment')"  :class="{active:isActive('/comment')}">评论</span>
          <span @click="tolink('collector')" :class="{active:isActive('/collector')}">收藏者</span>
        </div>
      </div>
      <router-view :tableData="tableData"></router-view>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      tableData: [],
      info: {},
      creator: {},
    };
  },
  methods: {
    tolink(path) {
      this.$router.push({name:path,query:{id:this.$route.query.id}});
    },
    getdata() {
      this.$request({
        url: "/playlist/detail",
        params: {
          id: this.$route.query.id,
        },
        withCredentials: true,
      }).then((success) => {
        this.info = success.data.playlist;
        this.creator = success.data.playlist.creator;
        this.tableData = success.data.playlist.tracks;
        this.tableData.forEach((i) => {
          i.dt = this.getTime(i.dt);
        });
      });
    },
  },
  computed: {
    getTime() {
      return function (time) {
        let min = Math.floor(time / 60000).toString()[1]
          ? Math.floor(time / 60000).toString()
          : "0" + Math.floor(time / 60000).toString();
        let sec = Math.floor(time % 60000)
          .toString()
          .slice(0, 2);
        return min + ":" + sec;
      };
    },
      isActive() {
      return function (path) {
        return this.$route.path.includes(path);
      };
    },
    getDate() {
      return function (t) {
        let date = new Date(t);
        let y = date.getFullYear();
        let m = date.getMonth() + 1;
        let d = date.getDate();
        if (d < 10) {
          d = "0" + d;
        }
        if (m < 10) {
          m = "0" + m;
        }
        return y + "-" + m + "-" + d;
      };
    },
  },
  mounted() {
    this.getdata();
    this.$root.eventhub.$on("gddata", () => {
      this.$request({
        url: "/playlist/detail",
        params: {
          id: this.$route.query.id,
        },
        withCredentials: true,
      }).then((success) => {
        this.info = success.data.playlist;
        this.creator = success.data.playlist.creator;
        this.tableData = success.data.playlist.tracks;
        this.tableData.forEach((i) => {
          i.dt = this.getTime(i.dt);
        });
      });
    });
  },
};
</script>

<style scoped>
.head {
  display: flex;
}
.imgbox {
  margin-right: 20px;
  width: 200px;
  height: 200px;
  background-color: gray;
  border-radius: 10px;
}
.imgbox img {
  width: 200px;
  height: 200px;
  border-radius: 10px;
}
.type {
  display: inline-block;
  width: 50px;
  height: 25px;
  line-height: 25px;
  color: #ec4141;
  text-align: center;
  border: 1px solid #ec4141;
  border-radius: 5px;
  font-size: 15px;
  font-weight: normal;
  margin-right: 10px;
}
.creator {
  margin: 20px 0;
}
.creator img {
  width: 50px;
  border-radius: 25px;
  vertical-align: middle;
  margin-right: 10px;
}
.buttons{
  margin-bottom: 20px;
}
.buttons span {
  display: inline-block;
  padding: 10px;
  text-align: center;
  font-size: 18px;
  border-radius: 19px;
}
.desc span{
  color: gray;
}
.play {
  width: 150px;

  background-color: #ec4141;
  color: white;
}
.etc {
  width: 100px;
  margin: 0 10px;
  border: 1px solid #ccc;
}
.count {
  margin-top: 20px;
}
.count span {
  display: inline-block;
  margin-right: 20px;
}
.bar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin: 40px 0;
}
.bar span {
  display: inline-block;
  height: 30px;
  line-height: 30px;
 
  margin-right: 20px;
  cursor: pointer;
}
.active{
   border-bottom: 4px solid #ec4141;
}
</style>